﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MyQuestMobApp.AuthenticationService;
using System.Windows.Media.Imaging;
using MyQuestMobApp.pages;

namespace MyQuestMobApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        List<BitmapImage> authPics;
        int authPic1 = 0;
        int authPic2 = 0;
        string type = "student";
        public static int user_id;
        // Constructor
        public MainPage()
        {
            
            InitializeComponent();
            
            getAllAuthPics();
        }
        

        public void getAllAuthPics()
        {
            
            var authenticationSrvClient = new AuthenticationSrvClient();
            authenticationSrvClient.getAllPicturesCompleted += new EventHandler<getAllPicturesCompletedEventArgs>(serviceClient_getAuthenticationCompleted);
            authenticationSrvClient.getAllPicturesAsync();
        }

        private void serviceClient_getAuthenticationCompleted(Object sender, getAllPicturesCompletedEventArgs e)
        {
            authPics = MyQuestApp.utils.ImageConvertor.ByteImageListToBitmapImageList(e.Result.ToList<byte[]>());
            populateAuthImages(authPics);
        }

        public void populateAuthImages(List<BitmapImage> authPics)
        {
            image1.Source = authPics[0];
            image2.Source = authPics[1];
            image3.Source = authPics[2];
            image4.Source = authPics[3];
            image5.Source = authPics[4];
            image6.Source = authPics[5];
            image7.Source = authPics[6];
            image8.Source = authPics[7];
            image9.Source = authPics[8];
            image10.Source = authPics[9];
            image11.Source = authPics[10];
            image12.Source = authPics[11];
            
        }

        private void login_Click(object sender, RoutedEventArgs e)
        {
            //this.Content = new student_mainPage(4, "janaton");
            //this.Content = new student_mainPage(4, "janaton");
            //NavigationService.Navigate(new Uri("/pages/offline_tracking.xaml", UriKind.Relative));

            //UNCOMMENT THIS
            var authenticationSrvClient = new AuthenticationSrvClient();
            authenticationSrvClient.authenticateCompleted += new EventHandler<authenticateCompletedEventArgs>(serviceClient_authenticateCompleted);
            authenticationSrvClient.authenticateAsync(Name_textBox.Text,SchoolName_textBox2.Text,type,authPic1,authPic2);
        }

         private void serviceClient_authenticateCompleted(Object sender, authenticateCompletedEventArgs e)
        {
            if (e.Result != 0 && type=="student") // Validation failed
            {
                user_id = e.Result;
                //NavigationService.Navigate(new Uri("/pages/student_mainPage.xaml", UriKind.Relative));
                this.Content = new student_mainPage(e.Result, "student_name");
            }
            else if (e.Result != 0 && type == "teacher")
            {
                user_id = e.Result;
                //NavigationService.Navigate(new Uri("/pages/teacher_mainPage.xaml", UriKind.Relative));
                this.Content = new teacher_mainPage(e.Result, "student_name");
            }
            else
            {
                resetPics();
                MessageBox.Show("Wrong name/school or passwords");
            }
        }

        private void image1_Tap(object sender,System.Windows.Input.GestureEventArgs e)
        {
            setPics(1);
        }

        public void setPics(int pic)
        {
            if (authPic1 != 0)
                authPic2 = pic;
            else
                authPic1 = pic;
        }
        public void resetPics()
        {
            authPic1 = 0;
            authPic2 = 0;
        }

        private void image2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(2);
        }

        private void image3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(3);
        }

        private void image12_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(12);
        }

        private void image4_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(4);
        }

        private void image5_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(5);
        }

        private void image6_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(6);
        }

        private void image11_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(11);
        }

        private void image7_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(7);
        }

        private void image8_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(8);
        }

        private void image9_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(9);
        }

        private void image10_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            setPics(10);
        }
        private void student_radioButton_Checked(object sender, RoutedEventArgs e)
        {
            type = "student";
        }

        private void teacher_radioButton_Checked(object sender, RoutedEventArgs e)
        {
            type = "teacher";
        }
    }
}